import requests
import pytest
import yaml
class Test_sec():
    token = ""
    addrid = ""
    def setup_method(self):
        print("方法级别，准备环境.......")
    def teardown_method(self):
        print("方法级别，恢复环境，比如关闭文件，断开数据库......")
    def setup_function(self):
        print("函数级别，准备环境.......")
    def teardown_function(self):
        print("函数级别，恢复环境，比如关闭文件，断开数据库......")
    def setup_class(self):
        print("类级别的初始化...")
    def teardown_class(self):
        print("类级别的钩子函数....")
    def read_yaml(filename):
        with open(filename,"r",encoding="utf-8") as f:
            data=yaml.safe_load(f)
            f.close()
            return data
    @pytest.mark.parametrize("data",read_yaml("yamluser.yaml"))
    def test_login(self,data):
        url = "http://10.59.9.12:7001/api/public/login"
        data = {"username": data.get("username"), "password": data.get("password")}
        res = requests.post(url=url,json=data)
        print(res.json())
        Test_sec.token = res.json()['data']['token']
        Test_sec.token = "Bearer " + Test_sec.token
        assert "会员" in res.text
        assert "13811033962" == res.json()['data']['username']
        assert res.json()['info'] == "ok"
    def test_sec_page_list(self):
        url = "http://10.59.9.12:7001/api/second?page=1&type=0&sort=1"
        res = requests.get(url=url)
        print(res.json())
        assert res.json()['info'] == "ok"
        assert res.json()['status'] == 1
    def test_goods_deatail(self):
        url = "http://10.59.9.12:7001/api/second/view?id=7"
        res = requests.get(url=url)
        print(res.json())
    def test_confirm(self):
        url = "http://10.59.9.12:7001/api/order/confirm?id=7&type=second&sku_id=&num=1&shipping_type=1"
        tou = {"Auth-Token":Test_sec.token}
        res = requests.get(url=url,headers=tou)
        print(res.json())
        assert res.json()['info'] == "ok"
    def test_pay_ok(self):
        url = "http://10.59.9.12:7001/api/order/create"
        data = {"id": "7", "type": "second", "address_id": 6, "store_id": 1, "shipping_type": 1,
                "bonus_id": "0", "payment": "balance", "remarks": "", "source": 1,
                "url": "http://10.59.9.12:7001/wap/pages/cart/confirm?id=7&sku_id=&num=1&type=second",
                "sku_id": "", "num": "1"}
        tou = {"Auth-Token": Test_sec.token}
        res = requests.post(url=url,json=data,headers=tou)
        print(res.json())

if __name__ == '__main__':
    pytest.main(["-s","A3mallmiaosha"])